package com.umbrella.manage.config;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

/**
*
*
* @author Wan, ChuanLong @ 06/27/2018
*/

@Configuration
@MapperScan(basePackages = "com.umbrella.plateform.dao", sqlSessionTemplateRef = "plateformSqlSessionTemplate")
public class SqlSession4PlateformConfig {
	
	@Bean(name = "plateformSqlSessionFactory")
	@Primary
	public SqlSessionFactory plateformSqlSessionFactory(@Qualifier("plateformDataSource")DataSource dataSource) throws Exception {
		
		SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
		bean.setDataSource(dataSource);
		return bean.getObject();
	}
	
	@Bean
	@Primary
	public PlatformTransactionManager  plateformTransactionManager(@Qualifier("plateformDataSource")DataSource dataSource) {
		return new DataSourceTransactionManager(dataSource);
	}

	@Bean(name = "plateformSqlSessionTemplate")
	@Primary
	public SqlSessionTemplate plateformSqlSessionTemplate(@Qualifier("plateformSqlSessionFactory")SqlSessionFactory sqlSessionFactory) throws Exception {
		return new SqlSessionTemplate(sqlSessionFactory);
	}
}
